package com.springmvc.init.handlerMapping;

import com.springmvc.controller.Controller;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class BeanNameHandlerMapping implements InstantiationAwareBeanPostProcessor,HandlerMapping {

    private final Map<String, Controller> controllerMap = new ConcurrentHashMap<>(8);
    @Override
    public Object gethandlerMapping(String requestUrl) {
        return controllerMap.get(requestUrl);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {

        if(beanName.startsWith("/")){
            controllerMap.put(beanName,(Controller)bean);
        }

        return true;
    }
}
